package com.jlastu.dlxy_androidapp.view.sell;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.jlastu.dlxy_androidapp.R;

import com.jlastu.dlxy_androidapp.adapter.NewsListAdapter;
import com.jlastu.dlxy_androidapp.base.BaseFragment;
import com.jlastu.dlxy_androidapp.contract.SellFirstContract;
import com.jlastu.dlxy_androidapp.entity.School;
import com.jlastu.dlxy_androidapp.entity.SchoolPhoto;
import com.jlastu.dlxy_androidapp.presenter.SellFirstPresenter;
import com.jlastu.dlxy_androidapp.view.MainActivity;
import com.jlastu.dlxy_androidapp.view.details.StoreActivity;

import java.util.ArrayList;
import java.util.List;

public class SellFirstFragment extends BaseFragment implements View.OnClickListener, SellFirstContract.ISellView {
    private RecyclerView mSellRecyclerView;
    private NewsListAdapter mRecyclerAdapter;
    private SellFirstContract.ISellPresenter mSellPresenter;
    private LinearLayout mLinearLayout1;
    private ImageButton mImageButton;
    private ImageView mImageView_1;
    private ImageView mImageView_2;
    private ImageView mImageView_3;
    private ImageView mImageView_4;


    private List<School> mSchoolList;

    private List<SchoolPhoto> mPhotoList;

    private TextView mToMore;


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_sell_home;
    }

    @Override
    protected void initView() {
        mSellPresenter = new SellFirstPresenter(this, getContext());
        mSchoolList = new ArrayList<>();
        mPhotoList = new ArrayList<>();
        mSellRecyclerView = mView.findViewById(R.id.rv_news);
        StaggeredGridLayoutManager staggeredGridLayoutManager =
                new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL);
        mSellRecyclerView.setLayoutManager(staggeredGridLayoutManager);
        mRecyclerAdapter = new NewsListAdapter(mSchoolList, mPhotoList);
        mSellRecyclerView.setAdapter(mRecyclerAdapter);
        mLinearLayout1 = mView.findViewById(R.id.search_view);
        mImageButton = mView.findViewById(R.id.search_view_1);
        mToMore = mView.findViewById(R.id.to_more);
        mImageView_1 = mView.findViewById(R.id.s_button1);
        mImageView_2 = mView.findViewById(R.id.s_button2);
        mImageView_3 = mView.findViewById(R.id.s_button3);
        mImageView_4 = mView.findViewById(R.id.s_button4);

    }

    @Override
    public void onResume() {
        super.onResume();
        mSellPresenter.getSchoolList();
    }

    @Override
    protected void initListener() {
        mImageButton.setOnClickListener(this);
        mToMore.setOnClickListener(this);
        mLinearLayout1.setOnClickListener(this);
        mImageView_1.setOnClickListener(this);
        mImageView_2.setOnClickListener(this);
        mImageView_3.setOnClickListener(this);
        mImageView_4.setOnClickListener(this);
    }

    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.search_view_1) {
            OnClickSell();
        } else if (id == R.id.to_more) {
            OnClickMore();
        } else if (id == R.id.search_view) {
            ToStore();
        } else if (id == R.id.s_button1) {
            Intent intent = new Intent(getActivity(),GoodsManageActivity.class);
            startActivity(intent);

        } else if (id == R.id.s_button2) {

        } else if (id == R.id.s_button3) {

        } else if (id == R.id.s_button4) {
            Intent intent = new Intent(getActivity(), StoreManageActivity.class);
            startActivity(intent);
        }
    }

    private void OnClickSell() {
        Intent intent = new Intent(getActivity(), MainActivity.class);
        startActivity(intent);
    }

    private void OnClickMore() {
        Intent intent = new Intent(getActivity(), MorenewsActivity.class);
        startActivity(intent);

    }

    private void ToStore() {
        Intent intent = new Intent(getActivity(), StoreActivity.class);
        startActivity(intent);
    }

    @Override
    public void showSchoolList(List<School> schoolList, List<SchoolPhoto> photoList) {
        mSchoolList = schoolList;
        mPhotoList = photoList;
        Log.d("", "showSchoolList " + mSchoolList.size());
        mRecyclerAdapter.updateDate(mSchoolList, mPhotoList);
    }
}
